package com.innovation.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.query.Query;
import com.innovation.data.model.Player;
import com.innovation.exception.DBException;

/**
 * 玩家数据操作
 * @author Administrator
 *
 */
public class PlayerDao extends BaseDao<Player> {
	
	private static final Logger log = Logger.getLogger(PlayerDao.class);
	
	/**
	 * 根据玩家id获取玩家基本数据
	 * @param id
	 * @return
	 */
	public Player getPlayer(final int id) {
		final Datastore ds = getDatastore();

		Player player = null;
		try {
			player = ds.get(Player.class, id);
		} catch (Exception e) {
			log.error("get player exception "+e.getMessage(), e);
			throw new DBException(e);
		} 
		return player;
	}
	
	/**
	 * 根据openId获取玩家基本数据
	 * @param openId
	 * @return
	 */
	public Player getPlayer(String openId) {
		final Datastore ds = getDatastore();

		Player player = null;
		try {
			Query<Player> query = ds.find(Player.class);
			query.field("openId").equal(openId);
			List<Player> list = query.asList();
			if( list.size()>0 ){
				return list.get(0);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new DBException(e);
		} 
		return player;
	}
}
